/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Felipe
 */
public class MissileSprite extends Sprite{
    private int m_Direction;
    private int mKX,  mKY;
    private int mLastDifference;
    private boolean mLastWT;
    private boolean isShooted;
    private MissileChangeListener listener;
	private int turning;
    
    public static final int TURNING_RIGHT = 1;
    public static final int TURNING_LEFT = 2;
    public static final int NOT_TURNING = 3;
    
    private static final int[] kTransformLookup = {
        Sprite.TRANS_NONE, Sprite.TRANS_NONE, Sprite.TRANS_NONE,
        Sprite.TRANS_MIRROR_ROT90,
        Sprite.TRANS_ROT90, Sprite.TRANS_ROT90, Sprite.TRANS_ROT90,
        Sprite.TRANS_MIRROR_ROT180,
        Sprite.TRANS_ROT180, Sprite.TRANS_ROT180, Sprite.TRANS_ROT180,
        Sprite.TRANS_MIRROR_ROT270,
        Sprite.TRANS_ROT270, Sprite.TRANS_ROT270, Sprite.TRANS_ROT270,
        Sprite.TRANS_MIRROR
    };
    private static final int[] kFrameLookup = {
        0, 1, 2, 1,
        0, 1, 2, 1,
        0, 1, 2, 1,
        0, 1, 2, 1
    };
    private static final int[] kxLookup = {
        0, 383, 707, 924,
        1000, 924, 707, 383,
        0, -383, -707, -924,
        -1000, -924, -707, -383
    };
    private static final int[] kyLookup = {
        1000, 924, 707, 383,
        0, -383, -707, -924,
        -1000, -924, -707, -383,
        0, 383, 707, 924
    };

    public MissileSprite(Image image, int frameWidth, int frameHeight) {
        super(image, frameWidth, frameHeight);
        defineReferencePixel(frameWidth / 2, frameHeight / 2);
        m_Direction = 0;
        isShooted = false;
    }

    public void turn(int difference) {
        m_Direction += difference;
        if (m_Direction < 0) {
            m_Direction += 16;
        }
        if (m_Direction > 15) {
            m_Direction %= 16;
        }
        setFrame(kFrameLookup[m_Direction]);
        setTransform(kTransformLookup[m_Direction]);

        mLastDifference = difference;
        mLastWT = true;
    }

    public void forward(int delta) {
        fineMove(kxLookup[m_Direction] * delta,
                -kyLookup[m_Direction] * delta);
        mLastDifference = delta;
        mLastWT = false;
    }

    public boolean isShoot(){
        return isShooted;
    }
    
    public void shoot(int delta, HelicopterSprite hel){
    	
    	if (!isShooted) {
    		isShooted = true;
    		fireFired();
    	}
        
        forward(delta);
//            if(this.collidesWith(hel, true)){
//                isShooted = false;
//            }
        
    }
    
    public void undo() {
        if (mLastWT) {
            turn(-mLastDifference);
        } else {
            forward(-mLastDifference);
        }
    }

    private void fineMove(int kx, int ky) {
        // First initialize mKX and mKY if they're
        // not close enough to the actual x and y.
        int x = getX();
        int y = getY();
        int errorX = Math.abs(mKX - x * 1000);
        int errorY = Math.abs(mKY - y * 1000);
        if (errorX > 1000 || errorY > 1000) {
            mKX = x * 1000;
            mKY = y * 1000;
        }
        // Now add the differences.
        mKX += kx;
        mKY += ky;
        // Set the actual position.
        setPosition(mKX / 1000, mKY / 1000);
    }

	public int getDirection() {
		return m_Direction;
	}
	
	private void fireFired() {
		if (listener != null) 
		listener.onMissileFire(this);
	}
	
	private void fireTurn() {
		if (listener != null)
		listener.onMissileBeginsTurn(this);
	}
	
	private void fireStopTurning() {
		if (listener != null)
		listener.onMissileStopsTurn(this);
	}
	
	public void setMissileListener(MissileChangeListener l) {
		listener = l;
	}

	public void setTurning(int missileTurn) {
		if (turning != missileTurn){
			turning = missileTurn;
			if (missileTurn == TURNING_LEFT || missileTurn == TURNING_RIGHT){
				fireTurn();
			}else{
				//STOP
				fireStopTurning();
			}
			
		}
	}
	
	public int getTurning(){
		return turning;
	}
}
